package com.aifeng.hassan_food.mine

import android.annotation.SuppressLint
import android.os.Bundle
import android.text.InputFilter
import android.text.method.PasswordTransformationMethod
import android.view.View
import com.aifeng.base.BaseActivity
import com.aifeng.common_ui.helper.*
import com.aifeng.hassan_food.BuildConfig
import com.aifeng.hassan_food.R
import com.aifeng.utils.bindLifeCycle
import com.aifeng.utils.subscribeBy
import com.aifeng.utils.subscribeByNoError
import com.aifeng.viewmodel.SettingViewModel
import kotlinx.android.synthetic.main.activity_setting_forget.*
import org.jetbrains.anko.toast
import org.koin.androidx.viewmodel.ext.android.getViewModel

class SettingForgetActivity : BaseActivity() {

    private var mCode: String = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_setting_forget)
        initTitle(title = "找回支付密码")
    }

    override fun getViewModel() = getViewModel<SettingViewModel>()

    override fun initTitle(title: String, name: String, divider: Boolean) {
        super.initTitle(title, name, divider)
        forget_name.filters = arrayOf<InputFilter>(NameLengthFilter(16))
        forget_new.transformationMethod = PasswordTransformationMethod.getInstance()
        forget_confirm.transformationMethod = PasswordTransformationMethod.getInstance()
    }

    @SuppressLint("SetTextI18n")
    override fun doClick(v: View) {
        super.doClick(v)
        when (v.id) {
            R.id.bt_yzm -> {
                getViewModel().getCode(getString("phone"), "2")
                    .bindLifeCycle(getLifecycleOwner())
                    .subscribeByNoError(
                        event = getViewModel(),
                        onSuccess = { inner ->
                            toast("已发送短信验证码至当前账号的手机上，请注意查收！")
                            mCode = inner

                            @Suppress("ConstantConditionIf")
                            if (BuildConfig.LOG_DEBUG) {
                                forget_yzm.setText(mCode)
                                forget_yzm.setSelection(forget_yzm.text.length)
                            }

                            getViewModel().countDown(count = 60)
                                .bindLifeCycle(getLifecycleOwner())
                                .subscribeBy(
                                    onNext = {
                                        bt_yzm.text = "${60 - it}s后重试"
                                        bt_yzm.isClickable = false
                                    },
                                    onComplete = {
                                        bt_yzm.text = "重新获取"
                                        bt_yzm.isClickable = true
                                    }
                                )
                        }
                    )
            }
            R.id.bt_sure -> {
                when {
                    forget_name.text.isBlank() -> {
                        forget_name.requestFocus()
                        toast("请输入真实姓名")
                        return
                    }
                    forget_id.text.isBlank() -> {
                        forget_id.requestFocus()
                        toast("请输入身份证号码")
                        return
                    }
                    forget_yzm.text.isEmpty() -> {
                        forget_yzm.requestFocus()
                        toast("请输入验证码")
                        return
                    }
                    forget_new.text.isEmpty() -> {
                        forget_new.requestFocus()
                        toast("请输入新支付密码")
                        return
                    }
                    forget_confirm.text.isEmpty() -> {
                        forget_confirm.requestFocus()
                        toast("请再次输入新支付密码")
                        return
                    }
                    !CommonUtil.IDCardValidate(forget_id.textString()) -> {
                        forget_id.requestFocus()
                        toast("请输入正确的身份证号码")
                        return
                    }
                    mCode.isEmpty() -> {
                        toast("请点击获取验证码")
                        return
                    }
                    forget_yzm.textString() != mCode -> {
                        forget_yzm.requestFocus()
                        forget_yzm.setText("")
                        toast("请输入正确的验证码")
                        return
                    }
                    forget_new.text.length < 6 -> {
                        toast("请输入6位数字支付密码")
                        return
                    }
                    forget_new.textString() != forget_confirm.textString() -> {
                        toast("支付密码输入不一致，请重新输入")
                        return
                    }
                }

                getViewModel().resetPay(
                    "id" to getString("userId"),
                    "realname" to forget_name.textString(),
                    "idno" to forget_id.toUpperCase(),
                    "code" to forget_yzm.textString(),
                    "paypwd" to forget_new.textString()
                )
                    .bindLifeCycle(getLifecycleOwner())
                    .subscribeByNoError(
                        event = getViewModel(),
                        onSuccess = {
                            toast("支付密码重置成功！")
                            putShared("password", forget_new.textString())
                            ActivityStack.screenManager.popActivities(this::class.java)
                        }
                    )
            }
        }
    }

}
